// pages/order/order.js
import {orderList} from "../../../api/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList:[],
    stateList:["全部","处理中","已确认","拒绝"],
    curIndex:0,
    textColor:""
  },
  //获取全部商品
async getOrderList(){
  console.log(wx.getStorageSync('info'))
  let res = await orderList({
    branchId:Number(wx.getStorageSync('info').belong)
  })
  console.log("订单",res)
  res.dataset.forEach(item => {
    item.year=item.realOrderTime.substr(0,4)
    item.yue=item.realOrderTime.substr(5,5)
    if(item.status == "已拒绝"){
      item.textColor="color1"
    }else if(item.status == "已处理"){
      item.textColor="color2"
    }else{
      item.textColor="color3"
    }
  });
  this.setData({
    orderList:res.dataset
  })
  console.log("date",this.data.orderList)
},
//点击导航
async changIndex(event){
  //console.log(event)
  let index = event.currentTarget.dataset.index
  this.setData({
    curIndex:index
  })
  if(index == 0){
    this.getOrderList()
  }else{
    let res = await orderList({
      status:index-1,
      branchId:Number(wx.getStorageSync('info').belong)
    })
    res.dataset.forEach(item => {
      item.year=item.realOrderTime.substr(0,4)
       item.yue=item.realOrderTime.substr(5,5) 
       if(item.status == "已拒绝"){
        item.textColor="color1"
      }else if(item.status == "已处理"){
        item.textColor="color2"
      }else{
        item.textColor="color3"
      }
    });
    this.setData({
      orderList:res.dataset
    })
  }
},
//点击到搜索订单页
goSearchOrder(){
  wx.navigateTo({
    url: '../searchOrder/searchOrder',
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
 async onLoad(options) {
    console.log("options",options)
    if(JSON.stringify(options) != "{}"){
      //调取搜索接口
      let res = await orderList({
        branchId:Number(wx.getStorageSync('info').belong),
        status:Number(options.status),
        name:options.name
      })
      console.log("搜索",res)
      res.dataset.forEach(item => {
        item.year=item.realOrderTime.substr(0,4)
         item.yue=item.realOrderTime.substr(5,5) 
         if(item.status == "已拒绝"){
          item.textColor="color1"
        }else if(item.status == "已处理"){
          item.textColor="color2"
        }else{
          item.textColor="color3"
        }
      });
      this.setData({
        orderList:res.dataset
      })
    }else{
      this.getOrderList()
    }
  },
  //点击到订单详情
  godetial(event){
   // console.log("orderid",event.currentTarget.dataset.orderid)
    wx.navigateTo({
      url: `../orderDetial/orderDetial?orderId=${event.currentTarget.dataset.orderid}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})